package com.example.dev_demo.mybatis.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class CustomCommentGenerator extends DefaultCommentGenerator {
    private boolean suppressAllComments = false;
    private boolean suppressDate = true;
    private String currentDateStr;

    public CustomCommentGenerator() {
        super();
        currentDateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        super.addConfigurationProperties(properties);
        this.suppressAllComments = StringUtility.isTrue(
                properties.getProperty("suppressAllComments"));
        this.suppressDate = StringUtility.isTrue(
                properties.getProperty("suppressDate"));
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass,
                                     IntrospectedTable introspectedTable) {
        if (suppressAllComments) {
            return;
        }

        topLevelClass.addJavaDocLine("/**");

        // 添加表注释
        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                topLevelClass.addJavaDocLine(" * " + remarkLine);
            }
        }

        // 添加类信息
        topLevelClass.addJavaDocLine(" *");
        topLevelClass.addJavaDocLine(" * 表名: " + introspectedTable.getFullyQualifiedTable());

        // 添加日期
        if (!suppressDate) {
            topLevelClass.addJavaDocLine(" * 生成时间: " + currentDateStr);
        }

        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }

        // 生成字段注释
        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
            field.addJavaDocLine(" */");

//            // 可选：添加Swagger注解
//            if (addSwaggerAnnotations) {
//                field.addJavaDocLine("@ApiModelProperty(value = \"" +
//                        introspectedColumn.getRemarks() + "\")");
//            }
        }
    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {
        // 不生成getter方法注释
    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {
        // 不生成setter方法注释
    }
}